"""Models for emencia.news"""
from datetime import datetime

from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from django.conf import settings

from tagging.fields import TagField
from filebrowser.fields import FileBrowseField
from tinymce.models import HTMLField


class Category(models.Model):
    """Category Model"""
    title = models.CharField(_('title'), max_length=150)
    slug = models.SlugField(_('slug'), help_text=_('Used for the URLs'))
    description = models.TextField(_('description'), blank=True)

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name = _('category')
        verbose_name_plural = _('categories')
        ordering = ('title',)

class Document(models.Model):
    """Document Model"""
    title = models.CharField(_('name'), max_length=100)
    description = models.TextField(_('description'), blank=True)
    document = FileBrowseField(_('document'), max_length=200,
                               initial_directory="/news/documents")

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name = _('document')
        verbose_name_plural = _('documents')
        ordering = ('title',)

class NewsPublishedManager(models.Manager):
    """News published manager"""

    def get_query_set(self):
        now = datetime.now()
        return super(NewsPublishedManager, self).get_query_set().filter(
            publication_start__lte=now, publication_end__gt=now,
            visibility=True).order_by('ordering', 'creation')


class News(models.Model):
    """News Model"""
    title = models.CharField(_('title'), max_length=150)
    slug = models.SlugField(_('slug'), help_text=_('Used for the URLs'))
    language = models.CharField(_('language'), max_length=2,
                                choices=settings.LANGUAGES, default='fr')
    description = HTMLField(_('description'), blank=True)
    content = HTMLField(_('content'))
    category = models.ManyToManyField(Category, verbose_name=_('category'),
                                      null=True)
    tags = TagField(_('tags'))
    author = models.ForeignKey(User, verbose_name=_('author'))
    documents = models.ManyToManyField(Document, verbose_name=_('documents'),
                                       blank=True, null=True)
    relations = models.ManyToManyField('self', verbose_name=_('relations'),
                                       blank=True, null=True)
    visibility = models.BooleanField(_('visibility'), default=True)
    ordering = models.IntegerField(_('ordering'), default=100)
    creation = models.DateTimeField(_('creation date'), auto_now_add=True)
    publication_start = models.DateTimeField(_('publication start'),
                                             default=datetime.now)
    publication_end = models.DateTimeField(_('publication end'),
                                           default=datetime(2042, 3, 15))
    objects = models.Manager()
    published = NewsPublishedManager()

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        return ('news_news_detail', (self.slug,))

    class Meta:
        verbose_name = _('news')
        verbose_name_plural = _('newses')
        ordering = ('title',)

